package com.lp.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lp.DefineException.DBHelperException;
import com.lp.entity.ProjectMember;
import com.lp.entity.User;
import com.lp.utils.DBHelper;
import com.lp.utils.RowToObject;

public class ProjectMemberDAO
{
	private DBHelper<ProjectMember> dbHelper;
	
	public ProjectMemberDAO()
	{
		dbHelper = new DBHelper<ProjectMember>();
	}
	
	public List<ProjectMember> findAllByUserId(User user)
	{
		String sql = "select * from project_member where project_userId = ?";
		
		return dbHelper.myExecuteQueryMulti(sql, new RowToProjectMember(), user.getId());
	}
	
	
	private class RowToProjectMember implements RowToObject<ProjectMember>
	{

		public ProjectMember rowToObject(ResultSet rs)
		{
			ProjectMember projectMember = new ProjectMember();
			
			try
			{
				projectMember.setId(rs.getInt("id"));
				projectMember.setProject_id(rs.getInt("project_id"));
				projectMember.setProject_userId(rs.getInt("project_userId"));
			}
			catch (SQLException e)
			{
				throw new DBHelperException(e.getMessage(),e);
			}
			
			return projectMember;
		}
		
	}
}
